package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.FoodDto;
import com.example.demo.entity.*;
import com.example.demo.mapper.FoodtvrelationMapper;
import com.example.demo.service.FoodService;
import com.example.demo.service.FoodtvrelationService;
import com.example.demo.service.TraceService;
import com.example.demo.service.VitaminService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

/**
* @author Atanycosts
* @description 针对表【foodTVRelation】的数据库操作Service实现
* @createDate 2023-04-11 19:03:34
*/
@Service
@Scope("prototype")
public class FoodtvrelationServiceImpl extends ServiceImpl<FoodtvrelationMapper, Foodtvrelation>
    implements FoodtvrelationService {


    @Autowired
    @Lazy
    private FoodtvrelationService foodtvrelationService;

    @Autowired
    private FoodService foodService;
    /**
     * 维生素
     */
    @Autowired
    private VitaminService vitaminService;

    /**
     * 微量元素
     */
    @Autowired
    private TraceService traceService;

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteFoodTVRelation(Long id) {
        foodtvrelationService.removeById(id);
        traceService.removeById(id);
        vitaminService.removeById(id);
    }

    /**
     * 新增
     * @param foodDto
     */
    @Override
    public void saveFood(FoodDto foodDto) {
        List<Food> food = foodDto.getFood();


        foodService.saveBatch(food);
        LambdaQueryWrapper<Food> foodLambdaQueryWrapper = new LambdaQueryWrapper();
        foodLambdaQueryWrapper.orderByDesc(Food::getUpdateTime).last("limit 1");
        Food lastOne = foodService.getOne(foodLambdaQueryWrapper);
        Long foodLastId = lastOne.getId();
        foodDto.setFoodId(foodLastId);

        List<Vitamin> vitamins = foodDto.getVitamins();
        vitamins.stream().map((item)->{
            item.setId(foodLastId);
            return item;
        }).collect(Collectors.toList());

        vitaminService.saveBatch(vitamins);

        List<Trace> traces = foodDto.getTraces();
        traces.stream().map((item)->{
            item.setId(foodLastId);
            return item;
        }).collect(Collectors.toList());
        traceService.saveBatch(traces);
        this.save(foodDto);
    }

    /**
     * 更新食物
     * @param foodDto
     */
    @Override
    public void updateFood(FoodDto foodDto) {

        foodService.updateBatchById(foodDto.getFood());
        traceService.updateBatchById(foodDto.getTraces());
        vitaminService.updateBatchById(foodDto.getVitamins());
        this.updateById(foodDto);
    }

    /**
     * 通过ID ,已复查
     * @param id
     * @return
     */
    @Override
    public FoodDto getByIdFood(Long id) {
        //传过来的是Relation的ID
        Foodtvrelation byId = this.getById(id);
        //返回的值
        FoodDto foodDto = new FoodDto();
        BeanUtils.copyProperties(byId, foodDto);

        // //拿到食物联系数据的所有ID

        LambdaQueryWrapper<Food> foodLambdaQueryWrapper = new LambdaQueryWrapper();
        foodLambdaQueryWrapper.eq(Food::getId, byId.getFoodId());
        List<Food> food = foodService.list(foodLambdaQueryWrapper);

        LambdaQueryWrapper<Vitamin> vitaminQueryWrapper = new LambdaQueryWrapper<>();
        vitaminQueryWrapper.eq(Vitamin::getId,byId.getFoodId());
        List<Vitamin> vitamin = vitaminService.list(vitaminQueryWrapper);

        LambdaQueryWrapper<Trace> traceQueryWrapper = new LambdaQueryWrapper<>();
        traceQueryWrapper.eq(Trace::getId,byId.getFoodId());
        List<Trace> trace = traceService.list(traceQueryWrapper);

        foodDto.setFood(food);
        foodDto.setVitamins(vitamin);
        foodDto.setTraces(trace);
        return foodDto;
    }
    // LambdaQueryWrapper<Foodtvrelation> foodtvrelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
    // foodtvrelationLambdaQueryWrapper.eq(Foodtvrelation::getFoodId, byId.getFoodId());

    /**
     * 食物分页查询 , 已复查
     * @param page
     * @param pageSize
     * @param foodName
     * @return
     */
    public Page<Food> getAllFood(int page, int pageSize, String foodName) {
        //构造分页构造器
        Page<Food> pageInfo = new Page<>(page,pageSize,true);
        //构造条件构造器
        LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(foodName), Food::getFoodName,foodName);
        //添加排序条件
        queryWrapper.orderByDesc(Food::getUpdateTime);

        //执行查询
        foodService.page(pageInfo,queryWrapper);
        return pageInfo;

    }

}



